package com.kgc.sbt.provider111;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.BuyVip;
import com.kgc.sbt.beans.UserCoupons;
import com.kgc.sbt.beans.Vip;
import com.kgc.sbt.mapper.UserInformationMapper;
import com.kgc.sbt.mapper.VipMapper;
import com.kgc.sbt.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Created on 2021/8/17.
 * <p>
 * Author: songhe
 * <p>
 * Description:
 */
@Service
@Slf4j
public class buyVip implements BuyVip {

    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserInformationMapper userInformationMapper;


    /**
     * @return : java.lang.String
     * @author : songhe
     * @date : 2021/8/21
     * @description :  1 查询会员信息
     * 2 判断当前会员是否到期，并改变会员状态
     */
    @Override
    public Vip queryDaysBuyUserId(Integer userId) {
        Date nowTime = new Date();
        long nowtime = nowTime.getTime();
        long endtime = vipMapper.queryDaysByUserId(userId).getTime();
        if (nowtime > endtime) {
            //更改会员表中的会员状态
            vipMapper.updateVipStatusTOZero(userId);
            //更改用户信息表中的会员状态
            userInformationMapper.updateVipStatusToOneByUserId(userId);
            return vipMapper.queryAllByUserId(userId);
        } else {
            //将date类型转换为String类型
            String time;
            SimpleDateFormat formater = new SimpleDateFormat();
            formater.applyPattern("yyyy-MM-dd HH:mm:ss");
            time = formater.format(vipMapper.queryDaysByUserId(userId));
            //更改用户信息表中的会员状态
            userInformationMapper.updateVipStatusToTwoByUserId(userId);
            return vipMapper.queryAllByUserId(userId);
        }

    }

    @Override
    public String getVip(Integer userId) {
        //判断是否已经是会员
        int vipStatus = vipMapper.selectVipStatusByUserId(userId);
        log.info("------会员状态：{},用户id: {}", vipStatus, userId);
        if (vipStatus == 1) {
            log.info("已是会员，转为续费");
            addVipTimes(userId);
            return "已是会员，成功续费一个月";
        } else {
            Vip getVip = new Vip();
            Date NowTime = new Date();
            getVip.setUserId(userId);
            getVip.setVipStatus(1);
            getVip.setStartTime(NowTime);
            long EndTime = NowTime.getTime() + (1000 * 60 * 60 * 24 * 10);
            Calendar time = Calendar.getInstance();
            time.setTimeInMillis(EndTime);
            getVip.setEndTime(time.getTime());
            vipMapper.updateByPrimaryKeySelective(getVip);
            log.info("------购买成功，{}", userId);

            redisUtils.set("userVip:"+userId,1,60*60*24*10);
            return "购买成功";
        }
    }

    @Override
    public String addVipTimes(Integer userId) {
        Vip getVip = new Vip();
        //获取到期时间
        Date updateTime = vipMapper.getEndTimeByUserId(userId);
        getVip.setUserId(userId);
        getVip.setVipStatus(1);
        long EndTime = updateTime.getTime() + (1000 * 60 * 60 * 24 * 10);
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(EndTime);
        getVip.setEndTime(time.getTime());
        vipMapper.updateByPrimaryKeySelective(getVip);
        log.info("------续费成功，{}", userId);

        Date nowTime =new Date();
        long newTime = nowTime.getTime()-EndTime;
        redisUtils.set("addUserVip:"+userId,1,newTime/1000+1);

        return "成功续费十天";
    }


}
